// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/avcodec_to_widget_7/nbr.cpp $
// $Rev: 250 $
// $Author: akio.miyoshi $
// $Date:: 2010-07-20 23:06:06 +0000#$

#include "nbr.h"

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

NetworkWaiter::NetworkWaiter(QNetworkReply *a_reply, bool a_buffer_mode)
    : m_mutex(QMutex::Recursive)
    , m_reply(a_reply)
    , m_buffer_mode(a_buffer_mode)
{
    Q_ASSERT(m_reply->thread() == QThread::currentThread());
    p_connect_slots();
}

NetworkWaiter::~NetworkWaiter()
{
    p_disconnect_slots();
}

void NetworkWaiter::p_connect_slots()
{
    p_disconnect_slots();
    connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(metaDataChanged()));
    connect(m_reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
    connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
}

void NetworkWaiter::p_disconnect_slots()
{
    disconnect(m_reply, 0, this, 0);
}

void NetworkWaiter::metaDataChanged()
{
	qDebug() << ("NetworkBatchRequest::metaDataChanged()");
}

void NetworkWaiter::replyReadyRead()
{
    qDebug()<< "NetworkBatchRequest::replyReadyRead()=====>m_reply->bytesAvailable():"
            << m_reply->bytesAvailable()
            << m_reply->header(QNetworkRequest::ContentLengthHeader)
            << m_reply->rawHeader("Content-Type");
    if(m_buffer_mode)
    {
        m_mutex.lock();
        m_bytes.append(m_reply->readAll());
        m_mutex.unlock();
    }
    m_eventLoop.exit(0);
}

void NetworkWaiter::replyFinished()
{
    qDebug() << ("NetworkBatchRequest::replyFinished()");
    m_eventLoop.exit(0);
}

void NetworkWaiter::wait()
{
    qDebug() << "[NetworkWaiter::wait()]";
    while(!m_reply->isFinished())
    {
        if(qApp->thread() == QThread::currentThread())
        {
            m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
        }
        else
        {
            msleep(10);
        }
    }
}

bool NetworkWaiter::waitBreakSize(int a_break_size)
{
    qDebug() << "[NetworkWaiter::waitBreakSize()]" << a_break_size;
    int v_break_size = a_break_size < 0 ? 0 : a_break_size;
    while(!m_reply->isFinished())
    {
        if(this->bytesAvailable()>=v_break_size)
        {
            break;
        }
        if(qApp->thread() == QThread::currentThread())
        {
            m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
        }
        else
        {
            msleep(10);
        }
    }
    return (!m_reply->isFinished() || this->bytesAvailable()>0);
}

qint64 NetworkWaiter::bytesAvailable()
{
    QMutexLocker v_lock(&m_mutex);
    Q_UNUSED(v_lock);
    if(m_buffer_mode)
    {
        return m_bytes.size();
    }
    else
    {
        return m_reply->bytesAvailable();
    }
}
QByteArray NetworkWaiter::readAll()
{
    QMutexLocker v_lock(&m_mutex);
    Q_UNUSED(v_lock);
    if(m_buffer_mode)
    {
        QByteArray v_result = m_bytes;
        m_bytes.clear();
        return v_result;
    }
    else
    {
        return m_reply->readAll();
    }
}
